﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mogre;
using MogreNewt;

namespace WastedMechanics
{
    static class Helper
    {

        public static Vector2 ProjXZ(Vector3 vec)
        {
            return new Vector2(vec.x, vec.z);
        }

        // BEGIN<do wywalenia>
        public static void DrawLine(string mname, Vector3 pt1, Vector3 pt2)
        {
            ManualObject manOb = Engine.SceneManager.CreateManualObject();
            manOb.Begin(mname, RenderOperation.OperationTypes.OT_LINE_LIST);

            manOb.Position(pt1 + new Vector3(0, 0.1f, 0));
            manOb.Position(pt2 + new Vector3(0, 0.1f, 0));

            manOb.End();

            SceneNode moNode = Engine.SceneManager.RootSceneNode.CreateChildSceneNode();
            moNode.Position = Vector3.ZERO;
            moNode.AttachObject(manOb);
        }
        // END<do wywalenia>


        public static Quaternion QuaternionFromAngle(Degree angle, Vector3 axis)
        {
            Quaternion q = new Quaternion();
            q.FromAngleAxis(angle, axis);
            return q;
        }

        public static float CosInterpolate(float min, float max, float i)
        {
            return min + (float)(-System.Math.Cos(i * System.Math.PI) + 1) * 0.5f * (max - min);
        }

        public static T Clamp<T>(T min, T max, T i) where T : IComparable<T>
        {
            if (i.CompareTo(min) < 0)
                return min;
            else if (i.CompareTo(max) > 0)
                return max;
            else return i;
        }

        public static bool PointInRect(float left, float top, float width, float height, Vector2 pt)
        {
            return pt.x >= left && pt.y >= top && pt.x <= left + width && pt.y <= top + height;
        }

        public static Vector2 GetScreenCoordinates(Vector3 point)
        {
            Vector3 projected = Engine.Camera.ProjectionMatrix * (Engine.Camera.ViewMatrix * point);
            return new Vector2(((projected.x * 0.5f) + 0.5f), 1.0f - ((projected.y * 0.5f) + 0.5f));
        }

        public static bool IsPointOnScreen(Vector2 point)
        {
            return point.x >= 0 && point.x <= 1 && point.y >= 0 && point.y <= 1;
        }

        public static float GetTextWidth(TextAreaOverlayElement text)
        {
            FontPtr font = FontManager.Singleton.GetByName(text.FontName);
            float textWidth = 0;
            float maxTextWidth = 0;

            for (int i = 0; i < text.Caption.Length; i++)
            {
                if (text.Caption[i] == 32)
                    textWidth += 0.5f;
                else if (text.Caption[i] == 10)
                {
                    maxTextWidth = System.Math.Max(textWidth, maxTextWidth);
                    textWidth = 0;
                }
                else
                    textWidth += font.GetGlyphAspectRatio(text.Caption[i]);                
            }
            maxTextWidth = System.Math.Max(textWidth, maxTextWidth);

            return maxTextWidth * text.CharHeight / Engine.Camera.AspectRatio;
        }

        static Random Random = new Random();

        public static int RandomInt(int max)
        {
            return Random.Next(max);
        }

    }
}
